package com.ruoyi.report_jkdj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jkdj.domain.bo.DjczlbgBaDetailBo;
import com.ruoyi.report_jkdj.domain.vo.DjczlbgBaDetailVo;
import com.ruoyi.report_jkdj.domain.DjczlbgBaDetail;
import com.ruoyi.report_jkdj.mapper.DjczlbgBaDetailMapper;
import com.ruoyi.report_jkdj.service.IDjczlbgBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 地基承载力试验检测报告详细Service业务层处理
 *
 * @author zyb
 * @date 2024-07-25
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class DjczlbgBaDetailServiceImpl implements IDjczlbgBaDetailService {

    private final DjczlbgBaDetailMapper baseMapper;

    /**
     * 查询地基承载力试验检测报告详细
     */
    @Override
    public DjczlbgBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询地基承载力试验检测报告详细列表
     */
    @Override
    public TableDataInfo<DjczlbgBaDetailVo> queryPageList(DjczlbgBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DjczlbgBaDetail> lqw = buildQueryWrapper(bo);
        Page<DjczlbgBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询地基承载力试验检测报告详细列表
     */
    @Override
    public List<DjczlbgBaDetailVo> queryList(DjczlbgBaDetailBo bo) {
        LambdaQueryWrapper<DjczlbgBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DjczlbgBaDetail> buildQueryWrapper(DjczlbgBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DjczlbgBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, DjczlbgBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getQualId() != null, DjczlbgBaDetail::getQualId, bo.getQualId());
        lqw.eq(bo.getXuh() != null, DjczlbgBaDetail::getXuh, bo.getXuh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancbw()), DjczlbgBaDetail::getJiancbw, bo.getJiancbw());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), DjczlbgBaDetail::getSymbolValue, bo.getSymbolValue());
        lqw.eq(bo.getNum1() != null, DjczlbgBaDetail::getNum1, bo.getNum1());
        lqw.eq(bo.getNum2() != null, DjczlbgBaDetail::getNum2, bo.getNum2());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), DjczlbgBaDetail::getJiszb, bo.getJiszb());
        lqw.eq(bo.getJiancjg() != null, DjczlbgBaDetail::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), DjczlbgBaDetail::getJiegpd, bo.getJiegpd());
        return lqw;
    }

    /**
     * 新增地基承载力试验检测报告详细
     */
    @Override
    public Boolean insertByBo(DjczlbgBaDetailBo bo) {
        DjczlbgBaDetail add = BeanUtil.toBean(bo, DjczlbgBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改地基承载力试验检测报告详细
     */
    @Override
    public Boolean updateByBo(DjczlbgBaDetailBo bo) {
        DjczlbgBaDetail update = BeanUtil.toBean(bo, DjczlbgBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DjczlbgBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除地基承载力试验检测报告详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
